﻿package com.oyjd.ybx.business.talk.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestUtils;

import com.oyjd.ybx.bean.vo.ProLineVO;
import com.oyjd.ybx.bean.vo.UserVO;
import com.oyjd.ybx.business.sys.service.SysService;
import com.oyjd.ybx.business.talk.service.TalkService;
import com.oyjd.ybx.business.talk.vo.CustVO;
import com.oyjd.ybx.fw.action.BaseAction;
import com.oyjd.ybx.fw.util.CodeUtil;
import com.oyjd.ybx.fw.util.OutUtil;
import com.oyjd.ybx.pub.Const;

/**
 * 
 * 在线交流Action控制层
 * 
 * @author <b>欧建东<br/>
 *         Email:oyjd@163.com</b>
 * @version 1.0
 */
@Action(value = "/talk")
@Results({ @Result(name = "viewBox", location = Const.JSP_pro_box), @Result(name = "viewTalk", location = Const.JSP_pro_talk) })
public class TalkAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 45354347027015039L;

	private static final Log log = LogFactory.getLog(TalkAction.class);
	private TalkService service;

	@Autowired
	public void setService(TalkService service) {
		this.service = service;
	}

	private SysService sysService;

	@Autowired
	public void setSysService(SysService sysService) {
		this.sysService = sysService;
	}

	/**
	 * 提供栏位登录信息
	 * 
	 * @return
	 */
	public String getUserByAccount() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		HttpServletRequest req = getRequest();
		String account = ServletRequestUtils.getStringParameter(req, "account", "");
		map.put("code", CodeUtil.SUCCESS);
		map.put("info", sysService.getUser(account));
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 进入专家答问界面
	 * 
	 * @return
	 */
	public String viewBox() {
		return "viewBox";
	}

	/**
	 * 进入实时对话框界面
	 * 
	 * @return
	 */
	public String viewTalk() {
		return "viewTalk";
	}

	/**
	 * 获取专家在线状态
	 * 
	 * @return
	 */
	public String getProStatus() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			List<ProLineVO> tmp = service.getPro();
			ProLineVO result = null;
			for (ProLineVO v : tmp) {
				if (v.getAccount().equals(userVO.getAccount())) {
					result = v;
					break;
				}
			}
			map.put("data", result);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 专家上线
	 * 
	 * @return
	 */
	public String addPro() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			HttpServletRequest req = getRequest();
			int type = ServletRequestUtils.getIntParameter(req, "type");
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.addPro(userVO.getAccount(), userVO.getName(), type);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 专家下线
	 * 
	 * @return
	 */
	public String removePro() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			service.removePro(userVO.getAccount());
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}

	/**
	 * 获取与专家对话的人物列表
	 * 
	 * @return
	 */
	public String getTalkerForPro() {
		Map<String, Object> map = CodeUtil.getJsonMap();
		try {
			UserVO userVO = (UserVO) session.get(CodeUtil.SessionKey);
			List<CustVO> tmp = service.getTalkerForPro(userVO.getAccount());

			map.put("data", tmp);
			map.put("code", CodeUtil.SUCCESS);
		} catch (Exception err) {
			log.error(err.getMessage(), err);
			map.put("code", CodeUtil.UNKOWN_ERR);
		}
		OutUtil.outJson(map, getResponse());
		return null;
	}
}
